package jyan.sango.crazy.core.xml;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import jyan.sango.crazy.core.card.Card;
import jyan.sango.crazy.core.card.CardColor;
import jyan.sango.crazy.core.card.CardName;
import jyan.sango.crazy.core.card.CardType;
import jyan.sango.crazy.core.debug.DEBUG;
import jyan.sango.crazy.core.debug.SangoException;

import org.dom4j.Element;

public class ReadCard {
	// 配置文件名
	private static final String filecard = "card.xml";
	private static Integer cardcategory = null;

	public static Integer getCardcategory() {
		return cardcategory;
	}

	public static void setCardcategory(Integer cardcategory) {
		ReadCard.cardcategory = cardcategory;
	}

	public static List<?> getCardsWithFileName(String filename) throws SangoException {
		List<Card> cardlist = new ArrayList<Card>();
		List<?> list = ReadFile.getElementList(getCardFile(filename),XMLType.Card);
		String str = null;
		Element data = null;
		Iterator<?> subitr = null;
		int i = 0;
		while (true) {
			Card card = new Card();
			data = (Element) list.get(i);
			DEBUG.Print(list.toString());
			subitr = data.elementIterator();
			DEBUG.Print(data.attribute("index").getName());
			str = data.attribute("index").getName();
			if (str.equals("index")) {
				str = data.attribute("index").getText();
				DEBUG.Print(str);
				card.setIndex(Integer.parseInt(str.trim()));
				i = Integer.parseInt(str.trim());
				if (i != -1) {
					++i;
				} else {
					break;
				}
			}
			while (subitr.hasNext()) {
				String strname = null;
				String strtext = null;
				data = (Element) subitr.next();
				strname = data.getName();
				DEBUG.Print("text:" + strname);
				strtext = data.getText();
				card = setToCard(card, strtext.trim(), strname.trim());
			}
			card = CardInsertCategory(card);
			cardlist.add(card);
		}
		return (List<?>) cardlist;
	}

	private static Card setToCard(Card card, String strtext, String strname) {
		if (strtext.equals("magnitude")) {
			card.setMagnitude(Integer.parseInt(strname.trim()));
		} else if (strtext.equals("color")) {
			card = CardCastColor(card, strtext, strname);
		} else if (strtext.equals("name")) {
			card = CardCastName(card, strtext, strname);
		}else if(strtext.equals("type")){
			card = CardCastType(card, strtext, strname);
		}else{
			
		}

		return card;
	}

	private static Card CardCastType(Card card, String strtext, String strname) {
		Integer ctype = null;
		ctype = CardType.getCardType(strname);
		card.setType(ctype);
		return card;
	}

	private static Card CardCastName(Card card, String strtext, String strname) {
		Integer cname = null;
		cname = CardName.getCardName(strname);
		card.setName(cname);
		return card;
	}

	private static Card CardCastColor(Card card, String strtext, String strname) {
		Integer color = null;
		color = CardColor.getCardColor(strname);
		card.setColor(color);
		return card;
	}

	private static Card CardInsertCategory(Card card) {
		Integer cate = getCardcategory();
		card.setCategory(cate);
		return card;
	}

	private static File getCardFile(String filename) {
		String tmpfilename = null;
		if (filename != null) {
			tmpfilename = filename;
		} else {
			tmpfilename = filecard;
		}
		File f = new File(tmpfilename);
		return f;
	}

}
